﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Ur;
using Sargon;


namespace Pixels.Interface
{
    public class Button : InterfaceElement
    {

        public Button(string label, int x, int y, int w, int h) {
            this.localPosition = new Rect(x, y, w, h);
            this.label = label;
        }

        public string textureID = "";
        public Color  color     = Color.white;

        public string label = "[btn]";

        public override void render() {
            if (!Visible) return;
            var spr = Render.spriteInBox(textureID, ScreenRect.scaleFromCenter(scale), true);
            spr.color = color;

            spr.zed = zed * 10f;

            var shadow = Render.textInBox(label, ScreenRect.move(1, 1), true);
            shadow.color = new Color(0, 0, 0, 1);
            var txt = Render.textInBox(label, ScreenRect, true);
            shadow.zed = zed * 10f + 0.2f;
            txt.zed = zed * 10f + 1f;

            if (clickSigma > 0f) clickSigma += Game.frame_time * 4;
            if (clickSigma > 1f) clickSigma = -1f;
        }

        public override void input(Sargon.InputState state) {
            if (!Visible) return;
            if (this.IsHilit ) if (Keys.Mouse1.pressed()) {
                if (Click != null) Click();
                startClick();
            }
        }

        protected float clickSigma = -1f;

        void startClick() {
            clickSigma = 0.001f;
        }

        protected float scale {
            get {
                if (clickSigma > 0f && clickSigma < 1f) {
                    //return clickSigma;
                    return 0.9f + Maths.abs(0.5f - clickSigma) * 0.2f;
                }
                return 1f;
            }
        }

        public event Action Click;

        public override void tick() {
            
        }

        public override bool respondsToHilite { get {
            return true; 
        }}
    }
}
